/*
 * FontManager.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#if !defined BRNlib_NoD3D
#include "FontManager.h"

namespace BRNlib
{
	namespace D3D
	{
		namespace Core
		{
			FontManager::FontManager(D3DXDevice *Device, LPCWSTR Name, int High)
			{
				this->pDevice = Device;
				this->Name = Name;
				this->High = High;
				Create();
			}

			TEXTMETRIC FontManager::GetTextMetric()
			{
				TEXTMETRIC FontProperties;

				#if defined BRNlib_D3D9
					GetFont()->GetTextMetricsW(&FontProperties);
				#endif
				#if defined BRNlib_D3D8
					HDC hDC = CreateCompatibleDC( NULL );
					GetTextMetrics(hDC, &FontProperties);
				#endif

				return FontProperties;
			}
			int FontManager::GetWidth()
			{
				TEXTMETRICW FontProperties = GetTextMetric();
				return FontProperties.tmAveCharWidth + 1 + FontProperties.tmOverhang;
			}
			int FontManager::GetHeight()
			{
				return GetTextMetric().tmHeight;
			}

			void FontManager::DrawText(int x, int y, char *Text, D3DCOLOR Color, bool Border, D3DCOLOR BGColor)
			{
				RECT Pos = { x, y, x + 120, y + 16 };
				DrawTextEx(Pos, Text, 256, Color, Border, BGColor);
			}

			void FontManager::DrawTextEx(RECT Pos, char* Text, DWORD Format, D3DCOLOR Color, bool Border, D3DCOLOR BGColor)
			{
				if(Border)
				{
					//Draw Border with BorderColor
					RECT BorderRect1 = { Pos.left-1, Pos.top, Pos.right-1, Pos.bottom };
					pFont->D3DX_DrawText( Text, -1, &BorderRect1, Format , BGColor );
					
					RECT BorderRect2 = { Pos.left+1, Pos.top, Pos.right+1, Pos.bottom };
					pFont->D3DX_DrawText( Text, -1, &BorderRect2, Format , BGColor );
					
					RECT BorderRect3 = { Pos.left, Pos.top-1, Pos.right, Pos.bottom-1 };
					pFont->D3DX_DrawText( Text, -1, &BorderRect3, Format , BGColor );
					
					RECT BorderRect4 = { Pos.left, Pos.top+1, Pos.right, Pos.bottom+1 };
					pFont->D3DX_DrawText( Text, -1, &BorderRect4, Format , BGColor );
				}

				//Draw Text with Color
				pFont->D3DX_DrawText( Text, -1, &Pos, Format , Color );
			}

			ID3DXFont *FontManager::GetFont()
			{
				return pFont;
			}

			void FontManager::OnPreReset()
			{
				Delete();
			}
			void FontManager::OnPostReset()
			{
				Create();
			}
			void FontManager::OnRelease()
			{
				Delete();
			}

			void FontManager::Create()
			{
				#if defined BRNlib_D3D9
					D3DXCreateFont( pDevice, High, 0, FW_NORMAL, 1, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, Name, &pFont );
				#endif
				#if defined BRNlib_D3D8
					HFONT Logical_Font_Characteristics = CreateFont(High, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, Name);

					if(Logical_Font_Characteristics != NULL)
					{
						D3DXCreateFont(pDevice, Logical_Font_Characteristics, &pFont);
						DeleteObject(Logical_Font_Characteristics);
					}
				#endif
			}
			void FontManager::Delete()
			{
				pFont->Release();
				pFont = NULL;
			}
		}
	}
}

#endif